package com.suyin.ximoon.dao.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.suyin.ximoon.dao.BaseDao;
import com.suyin.ximoon.model.bo.BaseModel;
import com.suyin.ximoon.plugin.mybatis.Criteria;
import com.suyin.ximoon.util.GenericsUtils;
public class BaseDaoImpl<T extends BaseModel> extends SqlSessionDaoSupport implements BaseDao<T> {

	private static final String MAPPER="com.suyin.ximoon.mapper.";
	protected Logger logger = LoggerFactory.getLogger(getClass());

	public SqlSessionTemplate sqlSessionTemplate;
	private Class<T> entityClass = GenericsUtils.getSuperClassGeneriacType(this.getClass());
	private String entityName = entityClass.getSimpleName();

	/**
	 * 构造一个namespace
	 * 
	 * @param mapperId
	 * @return
	 */
	protected String getSqlMapper(String mapperId) {
		logger.debug(" class name " + (MAPPER+entityName + "Mapper." + mapperId));
		return MAPPER+entityName+"Mapper" + "." + mapperId;
	}

	@Resource(name = "sqlSessionTemplate")
	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		this.sqlSessionTemplate = sqlSessionTemplate;
		super.setSqlSessionTemplate(this.sqlSessionTemplate);
	}

	/******** 抽出公用的方法 ********/

	public int insert(T entity) {
		int result = 0;
		result = this.sqlSessionTemplate.insert(getSqlMapper("insert"), entity);
		return result;
	}

	public void insert(List<T> list) {
		this.sqlSessionTemplate.insert(getSqlMapper("insertBatch"), list);
	}

	public void update(T entity) {
		this.sqlSessionTemplate.update(getSqlMapper("update"), entity);
	}

	public void update(List<T> list) {
		this.sqlSessionTemplate.update(getSqlMapper("updateBatch"), list);
	}

	public void deleteById(Integer id) {
		this.sqlSessionTemplate.delete(getSqlMapper("deleteById"), id);
	}
	

	public T queryOne(Integer id) {
		return this.sqlSessionTemplate.selectOne(getSqlMapper("queryOne"), id);
	}



	public List<T> query(Criteria criteria) {

		return this.sqlSessionTemplate.selectList(getSqlMapper("select_list_criteria"), criteria);
	}

	public T queryOne(Criteria criteria) {
		return this.sqlSessionTemplate.selectOne(getSqlMapper("select_one_criteria"), criteria);
	}

	public Integer count(Criteria criteria) {

		return this.sqlSessionTemplate.selectOne(getSqlMapper("select_count_criteria"), criteria);
	}

	public void delete(Criteria criteria) {

		this.sqlSessionTemplate.delete(getSqlMapper("delete_criteria"), criteria);
	}

	public void update(Criteria criteria) {
		this.sqlSessionTemplate.update(getSqlMapper("update_criteria"), criteria);
	}

	public void updateBatch(List<Criteria> criterias) {

		this.sqlSessionTemplate.update(getSqlMapper("updateBatch_criteria"), criterias);
	}

	public void insert(Criteria criteria) {
		this.sqlSessionTemplate.insert(getSqlMapper("insert_criteria"), criteria);

	}

	public void insertBatch(List<Criteria> criterias) {

		this.sqlSessionTemplate.insert(getSqlMapper("insertBatch_criteria"), criterias);
	}

	@Override
	public  List<T> queryByPage(T entity) {
		return this.sqlSessionTemplate.selectList(getSqlMapper("queryByPage"), entity);
	}


}
